package com.longe.controller;

import com.longe.common.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private JwtUtil jwtUtil;

    @RequestMapping("list")
    public String list(HttpServletRequest request){

        String token = request.getHeader("token");
        if(null == token || "".equals(token)){
            throw new RuntimeException("非法的请求");
        }
        //和前端约定传递token的时候,必须在token前面拼上字符long-
        if(!token.startsWith("long_")){
            throw new RuntimeException("非法的请求");
        }
        String finalToken = token.replace("long_", "");
        Claims claims = jwtUtil.parseJWT(finalToken);
        if(null == claims){
            throw new RuntimeException("非法的token或token已失效");
        }
        System.err.println("subject:" + claims.getSubject());
        System.err.println("issuedAt: " + claims.getIssuedAt());
        System.err.println("info: " + claims.get("info"));
        return "OK";
    }
}
